<template>
  <el-card class="m-b-20">
    <el-form ref="userForm" :model="user" :rules="rules" label-width="120px">
      <el-form-item hidden>
        <el-input v-model="user.userId" hidden />
      </el-form-item>
      <el-form-item label="登录账号" prop="loginName">
        <el-input v-model="user.loginName" readonly />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input v-model="user.userName" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="user.email" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="user.mobile" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="user.gender">
          <el-option
            v-for="item in optionOfGender"
            :key="item.dictCode"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthday">
        <el-date-picker
          v-model="user.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-check" @click="onSubmit">保 存</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { userUpdate } from '@/api/system/user'
import { validMobile } from '@/utils/validate'

export default {
  name: 'UserInfo',
  props: {
    userInfo: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    const validateUniq = (rule, value, callback) => {
      if (value === undefined || value === '') {
        rule.message = '该项不能为空'
        return callback(new Error(rule.message))
      }

      const param = rule.param.replaceParam
      rule.param[param] = value
      rule.param.userId = this.user.userId
      this.$http.get(rule.url, {
        params: rule.param
      }).then(response => {
        if (response.valid) {
          callback()
        } else {
          callback(new Error(rule.message))
        }
      }).catch(() => {
        callback()
      })
    }

    return {
      user: {
        userId: undefined,
        userName: '',
        loginName: '',
        email: '',
        mobile: '',
        gender: '',
        birthday: undefined
      },
      rules: {
        email: [
          { required: true, trigger: 'none', message: '邮箱不能为空' },
          { type: 'email', trigger: 'blur', message: '邮箱格式不正确' },
          {
            trigger: 'blur',
            param: {
              replaceParam: 'email'
            },
            validator: validateUniq,
            url: `${process.env.VUE_APP_MODULE_ADMIN_API}/public/user/unique`,
            message: '已存在该邮箱'
          }
        ],
        mobile: [
          { validator: validMobile, trigger: 'blur', message: '手机格式不正确' },
          {
            trigger: 'blur',
            param: {
              replaceParam: 'mobile'
            },
            validator: validateUniq,
            url: `${process.env.VUE_APP_MODULE_ADMIN_API}/public/user/unique`,
            message: '已存在该手机号'
          }]
      },
      optionOfGender: []
    }
  },
  watch: {
    userInfo: function(userInfo) {
      this.user = userInfo
    }
  },
  mounted() {
    this.user = this.userInfo
    this.getDictData()
  },
  methods: {
    getDictData() {
      this.$dictGroup('sys_user_sex').then(response => {
        this.optionOfGender = response
      }).catch(e => {})
    },
    onSubmit() {
      this.$refs.userForm.validate(valid => {
        if (valid) {
          userUpdate(this.user).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$store.dispatch('user/updateInfo', this.user)
          }).catch(() => {})
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}

.inline-block {
  display: inline-block;
}
</style>
